# -*- coding: utf-8 -*-
# 导入需要的包
import requests
import hashlib
import time
import logging


url = "http://61.145.229.28:7902/sms/v2/std/send_single"
timestamp = time.strftime("%m%d%H%M%S", time.localtime())
log_file_path = "./monitor_sms_balance.log"
logging.basicConfig(
    filename=log_file_path,
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
)


def sedMessages(messages):
    messages = messages
    uid = "JS9015"
    pwd = "256310"
    logging.info(f"正在处理...")
    tempPwd = uid.upper() + "00000000" + pwd + timestamp
    md5 = hashlib.md5()
    md5.update(tempPwd.encode("utf-8"))
    md5Pwd = md5.hexdigest()
    msg = f"RabbitMQ 产生告警：" + messages + "，请关注！"

    data = {
        "userid": uid,
        "pwd": md5Pwd,
        "mobile": 13421830635,
        "timestamp": timestamp,
        "svrtype": "SMS001",
        "content": msg,
    }

    try:
        response = requests.post(url, data=data)
        result = response.json()
        logging.info(f"告警发送成功，返回状态：\n" + result)
    except Exception as e:
        logging.error(f"告警发送失败：{e}")
        print(e)


messages = "Deliver (manual ack): 0.8/s"
sedMessages(messages)
